package com.extweb4j.web.controller;


import java.sql.Timestamp;
import java.util.Date;

import cn.dreampie.quartz.QuartzKey;
import cn.dreampie.quartz.QuartzKit;
import cn.dreampie.quartz.job.JobState;
import cn.dreampie.quartz.job.QuartzCronJob;
import cn.dreampie.quartz.job.QuartzOnceJob;

import com.extweb4j.core.anno.AuthAnno;
import com.extweb4j.core.anno.Log;
import com.extweb4j.core.controller.ExtController;
import com.extweb4j.core.kit.LoginKit;
import com.extweb4j.core.model.ExtJob;
import com.extweb4j.core.model.ExtMsg;
import com.extweb4j.core.model.ExtUser;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
/**
 * 定时任务控制器
 * @author Administrator
 *
 */
public class JobController extends ExtController{
	/**
	 * 列表
	 */
	@AuthAnno
	public void list(){
		int page = getParaToInt("page");
		int limit = getParaToInt("limit");
		String keywords = getPara("keywords");
		ExtUser user = LoginKit.getSessionUser(getRequest());
		Page<ExtJob> pageData = ExtJob.dao.paginateByKeywaord(page, limit,
				keywords,user.getId());
		for (ExtJob job : pageData.getList()) {
			job.set("uid", ExtUser.dao.findUserNameById(job.getStr("uid")));
		}
		renderJson(pageData);
	}
	/**
	 * 新增
	 */
	@Log("创建定时任务")
	@AuthAnno
	@Before(Tx.class)
	public void add(){
		String cron = getPara("cron");
		ExtUser user = LoginKit.getSessionUser(getRequest());
		ExtJob job = getExtModel(ExtJob.class);
		
		QuartzKey quartzKey = new QuartzKey(job.getInt("id"), job.getStr("uid"),job.getStr("msg_id"));
		if(cron.equals("0")){
			new QuartzOnceJob(quartzKey, job.getTimestamp("excute_time"), QuartzController.class).addParam("id",""+job.getInt("id")).start();
		}else{
	 	    //传递消息参数给quartz实体
			new QuartzCronJob(quartzKey,cron,QuartzController.class).addParam("id",""+job.getInt("id")).start();
		}
		job.set("create_time",  new Timestamp(new Date().getTime()));
		job.set("uid", user.getId());
		job.set("status", QuartzKit.getJob(quartzKey).getState());
		job.save();
		success("新建定时任务成功");
	}
	
	/**
	 * 编辑
	 */
	@Log("编辑定时任务")
	@AuthAnno
	@Before(Tx.class)
	public void edit(){
		ExtJob job = ExtJob.dao.findById(getParaToInt("id"));
		String cron  = getPara("cron");
		if(!job.getStr("cron").equals(cron)){
			QuartzKey quartzKey = new QuartzKey(job.getInt("id"), job.getStr("uid"),job.getStr("msg_id"));
			QuartzKit.stopJob(quartzKey);
			if(cron.equals("0")){
				new QuartzOnceJob(quartzKey, getParaToDate("excute_time"), QuartzController.class).addParam("id",""+job.getInt("id")).start();
			}else{
				new QuartzCronJob(quartzKey,cron,QuartzController.class).addParam("id",""+job.getInt("id")).start();
			}
			job.set("status", QuartzKit.getJob(quartzKey).getState());
		}
		job.update();
		
		success("编辑定时任务成功");
	}
	/**
	 * 删除
	 */
	@Log("删除定时任务")
	@AuthAnno
	@Before(Tx.class)
	public void delete(){
		ExtJob job = ExtJob.dao.findById(getPara("id"));
		//停止定时任务
		QuartzKey quartzKey = new QuartzKey(job.getInt("id"), job.getStr("uid"),job.getStr("msg_id"));
		QuartzKit.stopJob(quartzKey);
		job.set("status", JobState.STOPED);
		job.update();
		success("删除定时任务成功");
	}
	/**
	 * 暂停|重启|删除定时任务
	 */
	@Log("暂停|重启|删除定时任务")
	@AuthAnno
	@Before(Tx.class)
	public void action(){
		Integer[] job_ids = getParaValuesToInt("ids");
		String method = getPara("method");
		String result = "暂停";
		for (int i=0;i<job_ids.length;i++) {
			ExtJob job = ExtJob.dao.findById(job_ids[i]);
			QuartzKey quartzKey = new QuartzKey(job.getInt("id"), job.getStr("uid"),job.getStr("msg_id"));
			if(method.equals("pause")){
				QuartzKit.pauseJob(quartzKey);
				job.set("status", QuartzKit.getJob(quartzKey).getState());
			}else if(method.equals("resume")){
				QuartzKit.resumeJob(quartzKey);
				job.set("status", QuartzKit.getJob(quartzKey).getState());
				result = "重启";
			}else if(method.equals("delete")){
				QuartzKit.stopJob(quartzKey);
				job.set("status", JobState.STOPED);
				result = "删除";
			}else{
				error("参数错误");
			}
			job.update();
		}
		
		success(result+job_ids.length+"个定时任务");
	}
	
	/**
	 * 根据Id获取一条记录，编辑
	 */
	public void get(){
		ExtJob job = ExtJob.dao.findById(getParaToInt("id"));
		ExtMsg msg = ExtMsg.dao.findById(job.getStr("msg_id"));
		job.put("msg_title",msg.getStr("msg_title"));
		job.put("msg_content",msg.getStr("msg_content"));
		renderJson(new Record().set("success",true).set("data",job));
	}
}
